﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace G.Controls
{
    public static class ControlService
    {
        #region ProgressBarIsBegin
        public static readonly DependencyProperty ProgressBarIsBeginProperty = DependencyProperty.RegisterAttached("ProgressBarIsBegin", typeof(bool), typeof(ControlService), new PropertyMetadata(true, OnProgressBarIsBeginChanged));

        private static void OnProgressBarIsBeginChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
        {
            ProgressBar p = o as ProgressBar;
            if (p != null)
            {
                if (ControlService.GetProgressBarIsBegin(p))
                {
                    p.IsIndeterminate = true;
                    p.Visibility = Visibility.Visible;
                }
                else
                {
                    p.IsIndeterminate = false;
                    p.Visibility = Visibility.Collapsed;
                }
            }
        }


        public static bool GetProgressBarIsBegin(ProgressBar element)
        {
            if (element == null)
            {
                throw new ArgumentNullException("element");
            }

            return (bool)element.GetValue(ProgressBarIsBeginProperty);
        }

        public static void SetProgressBarIsBegin(ProgressBar element, bool value)
        {
            if (element == null)
            {
                throw new ArgumentNullException("element");
            }

            element.SetValue(ProgressBarIsBeginProperty, value);
        }
        #endregion

        #region 控件显示或隐藏，主要是配合绑定来做
        public static readonly DependencyProperty ControlVisibleProperty = DependencyProperty.RegisterAttached("ControlVisible", typeof(Visibility?), typeof(ControlService), new PropertyMetadata(null, OnControlVisibleBeginChanged));


        private static void OnControlVisibleBeginChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
        {
            UIElement u = o as UIElement;
            if (ControlService.GetControlVisible(u) == Visibility.Visible)
            {
                u.Visibility = Visibility.Visible;
            }
            else
            {
                u.Visibility = Visibility.Collapsed;
            }
        }

        public static Visibility? GetControlVisible(UIElement element)
        {
            if (element == null)
            {
                throw new ArgumentNullException("element");
            }

            return (Visibility?)element.GetValue(ControlVisibleProperty);
        }

        public static void SetControlVisible(UIElement element, Visibility? value)
        {
            if (element == null)
            {
                throw new ArgumentNullException("element");
            }

            element.SetValue(ControlVisibleProperty, value);
        }
        #endregion
    }
}
